package com.kymatrix.mpo.plus.bpm.server.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.kymatrix.mpo.plus.bpm.server.dao.category.BpmCategoryMapper;
import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmCategoryEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.PageQueryBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.PageQueryBpmCategoryResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.SaveBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.category.UpdateBpmCategoryReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmCategoryService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.exception.BusinessException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.kymatrix.mpo.plus.bpm.server.model.enums.ErrorCodeConstants.CATEGORY_CODE_DUPLICATE;
import static com.kymatrix.mpo.plus.bpm.server.model.enums.ErrorCodeConstants.CATEGORY_NAME_DUPLICATE;
import static com.kymatrix.mpo.plus.bpm.server.model.enums.ErrorCodeConstants.CATEGORY_NOT_EXISTS;

/**
 * BPM 流程分类 Service 实现类
 */
@Service
@Validated
public class BpmCategoryServiceImpl implements BpmCategoryService {

    @Resource
    private BpmCategoryMapper bpmCategoryMapper;

    @Override
    public String createCategory(SaveBpmCategoryReq createReqVO) {
        // 校验唯一
        BpmCategoryEntity category = bpmCategoryMapper.selectByName(createReqVO.getName());
        if (category != null) {
            throw new BusinessException(CATEGORY_NAME_DUPLICATE);
        }
        category = bpmCategoryMapper.selectByCode(createReqVO.getCode());
        if (category != null) {
            throw new BusinessException(CATEGORY_CODE_DUPLICATE);
        }
        
        BpmCategoryEntity entity = new BpmCategoryEntity();
        entity.setName(createReqVO.getName());
        entity.setCode(createReqVO.getCode());
        entity.setDescription(createReqVO.getDescription());
        entity.setStatus(createReqVO.getStatus());
        entity.setSort(createReqVO.getSort());
        bpmCategoryMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateCategory(UpdateBpmCategoryReq updateReqVO) {
        BpmCategoryEntity entity = this.bpmCategoryMapper.selectById(updateReqVO.getId());
        if (entity == null) {
            throw new BusinessException(CATEGORY_NOT_EXISTS);
        }
        BpmCategoryEntity category = bpmCategoryMapper.selectByName(updateReqVO.getName());
        if (category != null && !Objects.equals(category.getId(), entity.getId())) {
            throw new BusinessException(CATEGORY_NAME_DUPLICATE);
        }
        category = bpmCategoryMapper.selectByCode(updateReqVO.getName());
        if (category != null && !Objects.equals(category.getId(), entity.getId())) {
            throw new BusinessException(CATEGORY_CODE_DUPLICATE);
        }
        entity.setName(updateReqVO.getName());
        entity.setCode(updateReqVO.getCode());
        entity.setDescription(updateReqVO.getDescription());
        entity.setStatus(updateReqVO.getStatus());
        entity.setSort(updateReqVO.getSort());
        bpmCategoryMapper.updateById(entity);
    }

    @Override
    public void deleteCategory(String id) {
        // 删除
        bpmCategoryMapper.deleteById(id);
    }

    @Override
    public BpmCategoryEntity getCategory(String id) {
        return bpmCategoryMapper.selectById(id);
    }

    @Override
    public PageResp<PageQueryBpmCategoryResp> getCategoryPage(PageQueryBpmCategoryReq pageReqVO) {
        PageResp<PageQueryBpmCategoryResp> res = new PageResp<>();
        PageResp<BpmCategoryEntity> page = bpmCategoryMapper.selectPage(pageReqVO);
        res.setTotal(page.getTotal());
        for (BpmCategoryEntity entity : page.getList()) {
            res.getList().add(PageQueryBpmCategoryResp.fromEntity(entity));
        }
        return res;
    }

    @Override
    public List<BpmCategoryEntity> getCategoryListByCode(Collection<String> codes) {
        if (CollUtil.isEmpty(codes)) {
            return Collections.emptyList();
        }
        return bpmCategoryMapper.selectListByCode(codes);
    }

    @Override
    public List<BpmCategoryEntity> getCategoryListByStatus(String status) {
        return bpmCategoryMapper.selectListByStatus(status);
    }

}
